package com.example.yulu.applicationdemo;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yulu on 2017/8/13.
 */

public class DialogFragmentWindow extends DialogFragment {

	private static final String TAG = "DialogFragmentWindow";
	public static DialogFragmentWindow intance;


		private String[] title = new String[]{"Cool card", "Ok card", "Ok card", "Bad card","Ugly card"};
//	private String[] title = new String[]{"Cool card"};

	private String first = "";
	private String second = "";
	private String from = "";
	private ViewGroup group;
	private CardPagerAdapter mCardAdapter;
	private CardFragmentPagerAdapter mFragmentCardAdapter;
	private ShadowTransformer mCardShadowTransformer;
	private ShadowTransformer mFragmentCardShadowTransformer;
	private ImageView[] iv_dots;
	private ImageView iv_guide;
	private ImageView image_button;


	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//隐藏title
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		//设置dialog的 进出 动画
		getDialog().getWindow().setWindowAnimations(R.style.myDialogStyle);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		View view = inflater.inflate(R.layout.window, container);
		intance = this;
		first = getArguments().getString("first");
		second = getArguments().getString("second");
		from = getArguments().getString("from");
		group = (ViewGroup) view.findViewById(R.id.viewGroup);
		image_button = (ImageView) view.findViewById(R.id.image_button);
		ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		mCardAdapter = new CardPagerAdapter(getActivity());
		for (int i = 0; i < title.length; i++) {
			mCardAdapter.addCardItem(new CardItem(title[i], R.string.text_1));
		}
//		mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
//		mCardAdapter.addCardItem(new CardItem(R.string.title_2, R.string.text_1));
//		mCardAdapter.addCardItem(new CardItem(R.string.title_3, R.string.text_1));
//		mCardAdapter.addCardItem(new CardItem(R.string.title_4, R.string.text_1));
		mFragmentCardAdapter = new CardFragmentPagerAdapter(getActivity().getSupportFragmentManager(), dpToPixels(2, getContext()), getActivity());
		mCardShadowTransformer = new ShadowTransformer(viewPager, mCardAdapter);
		mFragmentCardShadowTransformer = new ShadowTransformer(viewPager, mFragmentCardAdapter);

		iv_dots = new ImageView[title.length];

		iv_guide = new ImageView(getActivity());

		if (title.length != 1) {
			for (int i = 0; i < title.length; i++) {
				iv_guide = new ImageView(getActivity());
				if (i == 0) {
					// 默认选中第一张图片
					iv_guide.setBackgroundResource(R.drawable.state_indicate_pressed_spot);
				} else {
					iv_guide.setBackgroundResource(R.drawable.state_indicate_nopressed_spot);
				}
				iv_dots[i] = iv_guide;
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
				layoutParams.leftMargin = 15; // 设置左边距为15个像素
				group.addView(iv_dots[i], layoutParams);
			}
		}


//		viewPager.setPageMargin(40);
		viewPager.setAdapter(mCardAdapter);


		viewPager.setPageTransformer(false, mCardShadowTransformer);
		viewPager.setOffscreenPageLimit(3);
		if (title.length != 1) {
			viewPager.setOnPageChangeListener(new GuidePageChangeListener());
		}

		image_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
					dismiss();
			}
		});

//		List<Fragment> fragments = getFragments();
//		AcoesMuscularesAdapter ama = new AcoesMuscularesAdapter(getChildFragmentManager(), fragments);
//		viewPager.setAdapter(ama);
//		viewPager.setPageTransformer(false, new ScaleTransformer());
//		viewPager.setPageTransformer(false, new AlphaTransformer());
//		if ("two".equals(from)) {//定位在第二个fragment
//			viewPager.setCurrentItem(1);
//		}
		return view;
	}


	//	按照一定比例显示
	@Override
	public void onStart() {
		super.onStart();
		//设置宽度顶满屏幕,无左右留白
		DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
		getDialog().getWindow().setLayout((int) (metrics.widthPixels / 1.2), (int) (metrics.heightPixels / 1.6));
	}

	private List<Fragment> getFragments() {//添加多个fragment
		List<Fragment> fList = new ArrayList<Fragment>();
		fList.add(MyPagerFragment.newInstance(this, first, 1));
		fList.add(MyPagerFragment.newInstance(this, second, 2));
		return fList;
	}


	class AcoesMuscularesAdapter extends FragmentPagerAdapter {

		private List<Fragment> fragments;

		public AcoesMuscularesAdapter(FragmentManager fm, List<Fragment> fragments) {
			super(fm);
			this.fragments = fragments;
		}

		@Override
		public Fragment getItem(int position) {
			return this.fragments.get(position);
		}

		@Override
		public int getCount() {
			return this.fragments.size();
		}
	}

	public static float dpToPixels(int dp, Context context) {
		return dp * (context.getResources().getDisplayMetrics().density);
	}

	/**
	 * 指引页面圆点监听器
	 */
	class GuidePageChangeListener implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// 设置小圆点的图片
			for (int i = 0; i < iv_dots.length; i++) {
				iv_dots[arg0].setBackgroundResource(R.drawable.state_indicate_pressed_spot);
				if (arg0 != i) {
					iv_dots[i].setBackgroundResource(R.drawable.state_indicate_nopressed_spot);
				}
			}
		}
	}
}
